// import md5 from 'crypto-js/md5';
export default{
    data(){
        return{
            expand:true,
            modal:false,
            list: [
            ],
            data:{
                id:'',
                name:'',
                title:'',
                limit:10,
                page:1,
                cateId:0,
                total:0,
                time:'',
            }
        }
    },
    created(){
        this.getData();
      },
      methods:{       
        search(){
            this.getData();
        },
        onpagesizechange(pageSize){
            console.log(pageSize)
            this.data.limit=pageSize;
            this.getData();
        },
        ok () {
            this.$Message.info('Clicked ok');
        },
        cancel () {
            this.$Message.info('Clicked cancel');
        },
        getData(){
            this.$http({
                method:'post',
                url:`http://10.31.153.35:3000/${this.module}/list`,
                data:this.data
            }).then(res=>{
               
                this.data.time=res.data.date;
                this.list=res.data.docs
                this.data.total=res.data.total
            })
        },
        show (params) {
        // this.modal=true;
        this.formValidate.title=params.row.title;
        this.formValidate.author=params.row.author;
        this.formValidate.content=params.row.content;
        this.formValidate.cateId=params.row.cateId;
        this.formValidate.id=params.row._id;
        console.log(this.formValidate)
        // if(params.row._id){//点击编辑
        // this.$http({
        //     method:'post',
        //     url:`http://10.31.153.35:3000/${this.module}/list`,
        //     data:{
        //         id:params.row._id
        //     }
        // }).then(res=>{  
        //      this.formValidate=res.data.docs[0];    
        // })
        //    console.log(params.row._id)
        //    

        //    }
            
        },
        remove (params) {
              this.$Modal.confirm({
                title: 'Title',
                content: '<p>确认删除？</p>',
                onOk: () => {
                    this.$http({
                        method:'delete',
                        url:`http://10.31.153.35:3000/${this.module}/data/${params.row._id}`
                    }).then(res=>{
                        this.getData();
                        // this.$Message.success("delete Success!");
                    })
                },
                onCancel: () => {
                    this.$Message.info('Clicked cancel');
                }
            });
       },
       handleSubmit(name){//必须输入正确的id 
        this.$http.get(`http://10.31.153.35:3000/cate/data/${this.formValidate.cateId}`).then(res=>{
            console.log(res.data,1111) //明天修改这里，返回的是id
            if(res.data){
                this.handleSubmitBack (name)//调用添加数据库模块
            }else{
                this.$Modal.confirm({
                    title: 'Title',
                    content: '<p>请选择正确的分类？</p>',
                    onOk: () => {
                        this.$Message.info('确定');
                    },           
            })
            // console.log(res.data.docs,1111)            
            }
        })
       },
             handleSubmitBack (name) {  

                 if(this.formValidate.id){
                    this.$http({
                        method: "put",
                        url:`http://10.31.153.35:3000/${this.module}/data/${this.formValidate.id}`,
                        data: this.formValidate,
                    }).then(res => {
                    this.getData();
                    //  console.log(res,'成功')
                     Object.assign(this.$data.formValidate
                        ,this.$options.data().formValidate);
                        this.modal=false;
                        this.$Message.success('updata Success!');
                    }) 
                 }else{
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        // this.formValidate.mail=md5(this.formValidate.mail).toString();
                        this.$http({
                            method: "post",
                            url: `http://10.31.153.35:3000/${this.module}/data`,
                            data: this.formValidate,
                        }).then(res => {
                         this.getData();
                         this.$Message.success('add Success!');
                         var cateId=this.formValidate.cateId;
                         Object.assign(this.$data.formValidate
                         ,this.$options.data().formValidate);
                         this.formValidate.cateId=cateId;
                         this.modal=false;
                        })                      
                    } else {
                        this.$Message.error('Fail!');
                    }               
                })
            }
            },
            handleReset (name) {
                // alert(11)
                Object.assign(this.$data.formValidate
                    ,this.$options.data().formValidate);
                // this.$refs[name].resetFields();
            },
            onChangePage(page){
                this.data.page=page;
                this.$http({
                    method:'post',
                    url:`http://10.31.153.35:3000/${this.module}/list`,
                    data:this.data,
                }).then(res=>{
                    console.log(res)
                    this.list=res.data.docs
                    this.data.total=res.data.total
                })
            }
        }
}